import jsencrypt from '../../components/jsencrypt/jsencrypt.vue'
import JSEncrypt from '../../components/jsencrypt/jsencrypt.min.js';
import CryptoJS from 'crypto-js';


// 加密公钥

// 加密
// 此加密公钥不能含有 '_' '-'字符串 参考：https://www.codenong.com/jsc71374f48fb3/

const globals = {
	rsaEncrypt: (msg) => {
		let key =
			'-----BEGIN PUBLIC KEY-----' +
			'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCczrUF2eMIA9wl67rE+fEypets1jR/Tn3vNff21cKDREU8nKZZx6OixvsZuPYOZeh6FxS6w2Wi/zV15Z1sx5HqDkMFJGahDFpsnjW2n5a2oD93lWasS3qaxSQDTL2SdwsQMNN7xE7/f8L6fHc9m557cbk5BKbhw5dFsXvqHQDWhQIDAQAB' +
			'-----END PUBLIC KEY-----'
		var encryptMsg = jsencrypt.setEncrypt(key, msg)
		return encryptMsg
	},
	randomString: (length) => {
		let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
		var result = '';
		for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
		return result;
	},
	nowTime: () => {
		let key = "tongkuxiaozhen";
		return Date.parse(new Date()) / 1000;
	},
	onRsaSign: (param) => {
		// 取 key
		let key = "tongkuxiaozhen";
		let keys = [];
		for (let key in param) {
			// 注意这里，要剔除掉 sign 参数本身
			if (key !== 'sign') {
				keys.push(key);
			}
		}

		// 参数名 ASCII 码从小到大排序（字典序）
		keys.sort();

		// 转成键值对
		let paramPair = [];
		for (let i = 0, len = keys.length; i < len; i++) {
			let k = keys[i];
			paramPair.push(k + '=' + encodeURIComponent(param[k])) // urlencode 编码
		}

		// 最后加上 key
		paramPair.push("salt=" + key);

		// 拼接
		let stringSignTemp = paramPair.join('&');

		let sign = CryptoJS.MD5(stringSignTemp).toString().toUpperCase();
		return sign
	}
}
export default globals;
//超长字符加密 支持汉字加密
// var ArrayData=jsencrypt.setLongEncrypt(key,"超长字符加密支持汉字加密");

// 存放所有需要用来签名的参数
// let param = {
// 	timestamp:''
// 	nonce:''
// 	data:''
// };
